#!/usr/bin/env sh

IMAGE=$1

if echo "$IMAGE" | grep -q "prebuilt-"; then
	exit 0
fi

if echo "$IMAGE" | grep -q "_archive"; then
	exit 0
fi

dir=$(basename "$IMAGE" .tar)

mkdir -p out/helper-images/"$dir"
tar -xf "$IMAGE" -C out/helper-images/"$dir"

archive=$(dirname "$IMAGE")/archive-$(basename "$IMAGE")
prebuilt=$(dirname "$IMAGE")/prebuilt-$(basename "$IMAGE")

rm -f "${archive}"
skopeo copy oci:"out/helper-images/${dir}" docker-archive:"${archive}"

if echo "$IMAGE" | grep -q "windows"; then
	prebuilt="$(dirname "$IMAGE")/$(basename "${prebuilt}" .tar).docker.tar.zst"
	rm -f "${prebuilt}"
	time zstd -9 -o "${prebuilt}" "${archive}"
	rm "${archive}"

	exit 0
fi

docker export -o "$prebuilt" "$(docker create "$(docker load <"${archive}" | grep "Loaded image ID:" | awk '{print $4}')")"
rm -rf "out/helper-images/${dir}"
rm "${archive}"

rm -f "${prebuilt}.xz"
time 7z a -mx8 -txz "${prebuilt}.xz" "${prebuilt}"
rm "$prebuilt"